<?php

namespace app\admin\dao;

use app\common\model\ConsultingSubsidy;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * ConsultingSubsidyDao 数据访问对象
 * Class ConsultingSubsidyDao
 * @package app\admin\dao
 * @method ConsultingSubsidy findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method ConsultingSubsidy insertActive(array $params, array $allow_field = [])
 * @method ConsultingSubsidy updateActive(ConsultingSubsidy $model, array $params, array $allow_field = [])
 * @method ConsultingSubsidy findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method ConsultingSubsidy updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(ConsultingSubsidy $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class ConsultingSubsidyDao
{
    use DaoTrait;

    /**
     * @var ConsultingSubsidy
     */
    private $baseModel;

    /**
     * ConsultingSubsidyDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = ConsultingSubsidy::class;
    }

    /**
     * 补贴列表
     * @param $provinceId
     * @param $cityId
     * @param $categoryId
     * @param $serverId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @param $page
     * @param $page_size
     * @return array
     */
    public function listSubsidies($provinceId, $cityId, $categoryId, $serverId, $startTime, $endTime, $kw, $page, $page_size): array
    {
        $tableName = ConsultingSubsidy::getTable();
        $query = ConsultingSubsidy::leftJoin('consulting c', "$tableName.consultingId=c.id")
            ->when(is_id($provinceId), fn($query) => $query->where("c.provinceId", $provinceId))
            ->when(is_id($cityId), fn($query) => $query->where("c.cityId", $cityId))
            ->when(is_id($categoryId), fn($query) => $query->where('c.categoryId', $categoryId))
            ->when(is_id($serverId), fn($query) => $query->where('c.consultingServerId', $serverId))
            ->when(is_numeric($startTime), fn($query) => $query->where("$tableName.created", '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where("$tableName.created", '<=', $endTime))
            ->when(not_blank($kw), fn($query) => $query->search('c.ask', $kw))
            ->tableField(true, $tableName)
            ->with([
                'consulting' => fn($query) => $query->field(['id', 'ask', 'lawyerId', 'provinceId', 'cityId', 'categoryId', 'lawyerMoney'])
                    ->with([
                        'province' => fn($query) => $query->shortArea(),
                        'city' => fn($query) => $query->shortArea(),
                        'lawyer' => fn($query) => $query->shortLawyer(),
                        'category' => fn($query) => $query->field(['id', 'name']),
                    ]),
            ])
            ->order("$tableName.id", 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    /**
     * 补贴列表统计
     * @param $provinceId
     * @param $cityId
     * @param $categoryId
     * @param $serverId
     * @param $startTime
     * @param $endTime
     * @param $kw
     * @return array
     */
    public function statisticsListSubsidies($provinceId, $cityId, $categoryId, $serverId, $startTime, $endTime, $kw): array
    {
        $tableName = ConsultingSubsidy::getTable();
        $query = ConsultingSubsidy::leftJoin('consulting c', "$tableName.consultingId=c.id")
            ->when(is_id($provinceId), fn($query) => $query->where("c.provinceId", $provinceId))
            ->when(is_id($cityId), fn($query) => $query->where("c.cityId", $cityId))
            ->when(is_id($categoryId), fn($query) => $query->where('c.categoryId', $categoryId))
            ->when(is_id($serverId), fn($query) => $query->where('c.consultingServerId', $serverId))
            ->when(is_numeric($startTime), fn($query) => $query->where("$tableName.created", '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where("$tableName.created", '<=', $endTime))
            ->when(not_blank($kw), fn($query) => $query->search('c.ask', $kw));

        return [
            'subsidy_count' => $query->count(),
            'subsidy_sum' => $query->sum('subsidy'),
        ];
    }
}
